/*
 * @Author: Aro
 * @Date: 2021-10-14 11:56:03
 */
// https://leetcode-cn.com/problems/binary-search/
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
    var n = nums.length;
    var head = 0;
    var tail = n - 1;
    while (head < tail) {
        var middle = head + Math.floor((tail - head) / 2);
        if (nums[middle] < target) {
            head = middle + 1;
        } else {
            tail = middle;
        }
    }
    if (nums[head] === target) return head;
    return -1;
};

console.log(search([-1,0,2,5,9,12], 999));


// https://leetcode-cn.com/problems/first-bad-version/
/**
 * Definition for isBadVersion()
 * 
 * @param {integer} version number
 * @return {boolean} whether the version is bad
 * isBadVersion = function(version) {
 *     ...
 * };
 */

/**
 * @param {function} isBadVersion()
 * @return {function}
 */
var solution = function (isBadVersion) {
    /**
     * @param {integer} n Total versions
     * @return {integer} The first bad version
     */
    return function (n) {

    };
};